package Datebase;

import javax.swing.table.DefaultTableCellRenderer;
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout.Alignment;
import javax.swing.text.StyledDocument;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class tcourse extends JFrame {
    public static void main(String[] args) {
        System.setProperty("sun.java2d.noddraw", "true");
        new tcourse("tno");
    }
    public tcourse(String tno){

        JFrame course = new JFrame("课程信息修改系统");
        URL resource = tcourse.class.getClassLoader().getResource("kun.png");
        Image image = new ImageIcon(resource).getImage();
        course.setIconImage(image);
        course.setSize(800, 600);
        course.setLocationRelativeTo(null);
        course.setLayout(null);

        try{
            Class.forName("oracle.jdbc.driver.OracleDriver");
            String dbURL = "jdbc:oracle:thin:@localhost:1521:XE";
            String user = "HR";
            String password = "root";
            Connection conn = DriverManager.getConnection(dbURL, user, password);
            String sql = "SELECT TEACH.TNO,COURSE.CNO,CNAME,CCREDIT,TERM FROM TEACH,COURSE WHERE TEACH.CNO=COURSE.CNO AND TEACH.TNO='"+tno+"'";
            PreparedStatement pstm = conn.prepareStatement(sql);
            ResultSet rs = pstm.executeQuery();

            int count = 0;
            while (rs.next()) {
                count++;
            }
            rs = pstm.executeQuery();

            Object[][] data = new Object[count][5];
            String[] title = {"教工号","课程号","课程名","学分","学期"};
            count = 0;
            while (rs.next()) {

                data[count][0] = (rs.getString("TNO"));
                data[count][1] = (rs.getString("CNO"));
                data[count][2] = (rs.getString("CNAME"));
                data[count][3] = (rs.getString("CCREDIT"));
                data[count][4] = (rs.getString("TERM"));
                count++;
            }
            JTable table = new JTable(data, title);
            JScrollPane scrollPane = new JScrollPane(table);
            DefaultTableCellRenderer cr = new DefaultTableCellRenderer();
            cr.setHorizontalAlignment(SwingConstants.CENTER);
            table.setDefaultRenderer(Object.class,cr);

            table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
            table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
            table.getColumnModel().getColumn(0).setPreferredWidth(200);
            table.getColumnModel().getColumn(1).setPreferredWidth(200);
            table.getColumnModel().getColumn(2).setPreferredWidth(200);
            table.getColumnModel().getColumn(3).setPreferredWidth(200);
            table.getColumnModel().getColumn(4).setPreferredWidth(200);
            table.setRowHeight(80);
            table.setFont(new Font("微软雅黑",Font.BOLD,13));
            scrollPane.setBounds(90, 50, 600, 185);
            course.add(scrollPane);

        }catch (ClassNotFoundException cnfe) {
            cnfe.printStackTrace();
            JOptionPane.showMessageDialog(null, "数据源错误", "错误", JOptionPane.ERROR_MESSAGE);
        }catch (SQLException sqle) {
            sqle.printStackTrace();
            JOptionPane.showMessageDialog(null, "数据操作错误", "错误", JOptionPane.ERROR_MESSAGE);
        }

        JLabel change = new JLabel("请输入你要修改课程信息的课程号：");
        change.setFont(new Font("微软雅黑",Font.PLAIN,18));
        change.setBounds(90,320,300,60);
        course.add(change);

        JTextField textField5 = new JTextField();
        textField5.setFont(new Font("微软雅黑",Font.PLAIN,18));
        textField5.setBounds(410,340,150,30);
        textField5.setColumns(10);
        course.add(textField5);

        JButton button = new JButton("确认");
        button.setFont(new Font("微软雅黑",Font.PLAIN,18));
        button.setBounds(590,340,80,30);
        course.add(button);

        JButton button7 = new JButton("课程名");
        button7.setFont(new Font("微软雅黑",Font.PLAIN,18));
        button7.setBounds(90,435,100,40);

        JButton button8 = new JButton("学分");
        button8.setFont(new Font("微软雅黑",Font.PLAIN,18));
        button8.setBounds(320,435,100,40);

        JButton button9 = new JButton("学期");
        button9.setFont(new Font("微软雅黑",Font.PLAIN,18));
        button9.setBounds(570,435,100,40);

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    Class.forName("oracle.jdbc.driver.OracleDriver");
                    String dbURL1 = "jdbc:oracle:thin:@localhost:1521:XE";
                    String user1 = "HR";
                    String password1 = "root";
                    Connection conn1 = DriverManager.getConnection(dbURL1, user1, password1);
                    String sql1 = "SELECT CNO FROM EXAM WHERE CNO = '"+textField5.getText()+"'";

                    PreparedStatement pstm1 = conn1.prepareStatement(sql1);
                    ResultSet rs1 = pstm1.executeQuery();

                    int count1 = 0;
                    while (rs1.next()) {
                        count1++;
                    }

                    if(count1 == 0)
                    {
                        JOptionPane.showMessageDialog(null, "课程号不存在!",
                                "提示", JOptionPane.WARNING_MESSAGE);
                    }


                else{
                    course.repaint();
                    course.add(button7);
                    course.add(button8);
                    course.add(button9);

                    button7.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            new tmex(textField5.getText(), button7.getText());
                        }
                    });

                    button8.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            new tmex(textField5.getText(), button8.getText());
                        }
                    });

                    button9.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            new tmex(textField5.getText(), button9.getText());
                        }
                    });
                }
                } catch (ClassNotFoundException cnfe) {
                    cnfe.printStackTrace();
                    JOptionPane.showMessageDialog(null, "数据源错误", "错误", JOptionPane.ERROR_MESSAGE);
                } catch (SQLException sqle) {
                    sqle.printStackTrace();
                    JOptionPane.showMessageDialog(null, "数据操作错误", "错误", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
        course.setVisible(true);
    }
}
